package com.yzx.lifeassistants.model.impl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.yzx.lifeassistants.bean.PartTimeInfo;
import com.yzx.lifeassistants.model.IQueryPartTimeInfo;
import com.yzx.lifeassistants.model.callback.IQueryPartTimeInfoCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;

/**
 * @Description: 查询兼职信息详情
 * @author: yzx
 * @time: 2016-3-22 下午9:33:50
 */
public class QueryPartTimeInfoImpl implements IQueryPartTimeInfo {

	public static final String TAG = "QueryPartTimeInfoImpl";
	private IQueryPartTimeInfoCB queryCallBack;// 回调

	public QueryPartTimeInfoImpl(Context context,
			IQueryPartTimeInfoCB queryCallBack) {
		this.queryCallBack = queryCallBack;
	}

	/**
	 * 
	 * @Description: 查询兼职信息详情
	 */
	@Override
	public void queryPartTimeInfo(String url) {
		HttpClientUtil.sendRequestWithReferer(url, "http://fz.1010jz.com/job/",
				null, new IHttpCallBack() {

					@Override
					public void onFinish(String response) {
						LogcatUtils.i(TAG, response);
						PartTimeInfo partTimeInfo = new PartTimeInfo();
						if (null == response || "".equals(response)) {
							LogcatUtils.e(TAG, "获取兼职信息详情失败");
							queryCallBack
									.queryError(IQueryPartTimeInfoCB.RESULR_NULL);
						} else {
							Document doc = Jsoup.parse(response);
							Element divElement = doc.select("div.d_left")
									.first();
							divElement.select("span.red_font").remove();
							String title = divElement.select("h2").first()
									.text();
							LogcatUtils.i(TAG, title);
							if (haveData(title)) {
								partTimeInfo.setTitle(title);
							}
							Elements elements = divElement.select("span");
							String time = elements.get(0).text();
							LogcatUtils.i(TAG, time);
							if (haveData(time)) {
								partTimeInfo.setTime(time);
							}
							String place = elements.get(1).select("a").first()
									.text();
							LogcatUtils.i(TAG, place);
							if (haveData(place)) {
								partTimeInfo.setPlace(place);
							}
							int i;
							if (divElement.toString().contains("职位性质")) {
								String jobNature = elements.get(2).text();
								LogcatUtils.i(TAG, jobNature);
								if (haveData(jobNature)) {
									partTimeInfo.setJobNature(jobNature);
								}
								i = 1;
							} else {
								i = 0;
							}
							String company = "";
							Element companyElement = elements.get(2 + i);
							LogcatUtils.i(TAG, companyElement.toString());
							if (companyElement.toString().contains("href")) {
								company = companyElement.select("a").first()
										.text();
								partTimeInfo.setIsVerify(true);
							} else {
								company = companyElement.text().replace(
										"公司名称：", "");
								partTimeInfo.setIsVerify(false);
							}
							if (haveData(company)) {
								partTimeInfo.setCompany(company);
							}
							String industry = elements.get(3 + i).text();
							LogcatUtils.i(TAG, industry);
							if (haveData(industry)) {
								partTimeInfo.setIndustry(industry);
							}
							String companyType = elements.get(4 + i).text();
							LogcatUtils.i(TAG, companyType);
							if (haveData(companyType)) {
								partTimeInfo.setCompanyType(companyType);
							}
							String companyScale = elements.get(5 + i).text();
							LogcatUtils.i(TAG, companyScale);
							if (haveData(companyScale)) {
								partTimeInfo.setCompanyScale(companyScale);
							}
							Element detailElement = divElement.select(
									"div.d_content").first();
							if (detailElement.toString().contains("img")) {
								String telUrl = detailElement.select("img")
										.first().attr("src");
								LogcatUtils.i(TAG, telUrl);
								if (haveData(telUrl)) {
									partTimeInfo.setTelUrl(telUrl);
								}
								detailElement.select("img").first().remove();
							}
							String detail = detailElement.toString();
							if (detail.contains("手机：")) {
								detail = detail.replace("手机：", "");
							}
							LogcatUtils.i(TAG, detail);
							if (haveData(detail)) {
								partTimeInfo.setDetail(detail);
							}
							if (haveData(partTimeInfo.getTitle())
									&& haveData(partTimeInfo.getTime())
									&& haveData(partTimeInfo.getPlace())
									&& haveData(partTimeInfo.getCompany())
									&& haveData(partTimeInfo.getCompanyType())
									&& haveData(partTimeInfo.getCompanyScale())
									&& haveData(partTimeInfo.getIndustry())) {
								LogcatUtils.i(TAG, "获取兼职信息详情成功");
								queryCallBack.querySuccess(partTimeInfo);
							} else {
								LogcatUtils.e(TAG, "获取兼职信息详情失败");
								queryCallBack
										.queryError(IQueryPartTimeInfoCB.RESULR_NULL);
							}
						}
					}

					@Override
					public void onError(Exception e) {
						LogcatUtils.e(TAG, "获取兼职信息详情异常");
						if (null != e.getMessage()) {
							LogcatUtils.e(TAG, e.getMessage());
						}
						queryCallBack
								.queryError(IQueryPartTimeInfoCB.QUERY_ERROR);
					}
				});
	}

	/**
	 * 
	 * @Description: 是否有数据
	 */
	private Boolean haveData(String data) {
		if (null == data || "".equals(data)) {
			return false;
		} else {
			return true;
		}
	}
}
